package com.factory.test.base;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.yecontech.sdk.base.YTSdkService;

import es.dmoral.toasty.Toasty;


public class BaseActivity extends AppCompatActivity {

    protected MyApplication myApplication;
    protected Context context;
    protected ActionBar actionBar;
    protected Handler handler = new Handler();
    protected YTSdkService sdkService;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;
        myApplication = MyApplication.getMyApplication();
        sdkService = MyApplication.getSdkService(context);

        actionBar = getSupportActionBar();		    			//获取actionBar
        if(actionBar != null){
            actionBar.setDisplayShowCustomEnabled(true);		//显示自定义控件
            actionBar.setDisplayShowHomeEnabled(true);			//显示logo
            actionBar.setDisplayHomeAsUpEnabled(true);			//显示小返回图标


            /*
            View view = LayoutInflater.from(context).inflate(R.layout.base_actionbar_title, null);
            titleView = (TextView) view.findViewById(R.id.title);
            ActionBar.LayoutParams layouts = new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT, ActionBar.LayoutParams.MATCH_PARENT);
            layouts.gravity = Gravity.CENTER;
            titleView.setTextColor(getResources().getColor(R.color.actionBarTitleText));
            actionBar.setCustomView(view, layouts);


            //多语言导致 actionBar不显
            if(actionBar.getTitle() != null) {
                actionBar.setTitle(getTitleString());
            }
            */
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home: {
                onBackPressed();
                break;
            }
        }
        return super.onOptionsItemSelected(item);
    }


    public void showError(String msg){
        Toasty.error(context,  msg, Toast.LENGTH_LONG, true).show();
    }

    public void showSuccess(String msg){
        Toasty.success(context,  msg, Toast.LENGTH_SHORT, true).show();
    }

    public void showWarning(String msg){
        Toasty.warning(context,  msg, Toast.LENGTH_LONG, true).show();
    }



    /**
     * 判断字符串是否为空,可接收一个多或多个数据<br>
     * 只要其中一个为空,都会返回true
     * @return
     */
    public static boolean isEmpty(String...str){
        for(String s:str){
            if (s == null || s.trim().length() == 0) {
                return true;
            }
        }
        return false;
    }


    /**
     * 判断字符串是否不为空,可接收一个多或多个数据<br>
     * 只要其中一个为空,都会返回false
     * @return
     */
    public static boolean isNotEmpoty(String... str){
        for (String s : str) {
            if (s == null || s.trim().length() == 0) {
                return false;
            }
        }
        return true;
    }





    /**
     * 默认的控件抖动动画
     * @param counts	1秒抖动的次数
     * @return
     */
    public static Animation shakeAnimation(int counts){
        return shakeAnimation(counts,1000);
    }


    /**
     * 控件抖动动画
     * @param counts	抖动次数
     * @param duration	动画时间(毫秒)
     * @return
     */
    public static Animation shakeAnimation(int counts, int duration) {
        Animation translateAnimation = new TranslateAnimation(0, 0, 0, 10);
        translateAnimation.setInterpolator(new CycleInterpolator(counts));
        translateAnimation.setDuration(duration);
        return translateAnimation;
    }

    /**
     * 隐藏软键盘(只适用于Activity，不适用于Fragment)
     */
    public static void hideSoftKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }


    public static void hideSoftKeyboard(Activity activity, View currentFocus) {
        try {
            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(currentFocus.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        } catch (Exception e) {
            //e.printStackTrace();
        }
    }


    protected void fullscreen(boolean show) {
        if (show) {
            if(actionBar != null) {
                actionBar.show();
            }
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);   //显示状态栏
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
        } else {
            if(actionBar != null) {
                actionBar.hide();
            }
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION); //隐藏状态栏
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN);
        }
    }

}
